package com.wmm.servlet.classes;

import com.wmm.bean.Classes;
import com.wmm.bean.PageBean;
import com.wmm.service.ClassesService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AddClassServlet", urlPatterns = "/addClass")
public class AddClassServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.  接收前端传来的班级信息参数
        String classNo = request.getParameter("classNo");
        String className = request.getParameter("className");
        String grade = request.getParameter("grade");
        String teacher = request.getParameter("teacher");
        String slogan = request.getParameter("slogan");

        Classes myclass = new Classes();
        myclass.setClassNo(classNo);
        myclass.setClassName(className);
        myclass.setClassNumber(0);
        myclass.setClassSlogan(slogan);
        myclass.setClassTeacher(teacher);
        myclass.setGrade(grade);
        ClassesService classService = new ClassesService();
        boolean add = classService.addClassesServer(myclass);
        if(add) {
            System.out.println("注册成功");
            PageBean<Classes> queryList = classService.findClassesByPage("1","5");
            HttpSession session = request.getSession();
            session.setAttribute("queryClassList",queryList);
            response.sendRedirect("showClass.jsp");
        } else {

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
